#!/usr/bin/python

import sys
import shlex
import json
import os
import re
import subprocess
import commands

def mysql_version_get():
    try:
        (status, output) = commands.getstatusoutput('su - mysql -c "mysql -V"')
        if status == 0:
            results_str=output
            results_lt=re.split(r" |,",results_str)
            mysql_version=results_lt[5]
        else:
            (status, output) = commands.getstatusoutput('mysql -V')
            results_str=output
            results_lt=re.split(r" |,",results_str)
            mysql_version=results_lt[5]
    except Exception as e:
        mysql_version=""

    return mysql_version

def oracle_version_get():
    try:
        results=os.popen('su - oracle -c "sqlplus -V"')
        results_str=results.read()
        results_lt=re.split(r" ",results_str)
        oracle_version=results_lt[2]
    except Exception as e:
        oracle_version=""

    return oracle_version
    results.close()

def jboss_version_get():
    try:
        results=os.popen('cat /upjas/*/modules/system/layers/base/org/jboss/as/product/upjas/dir/META-INF/MANIFEST.MF | grep JBoss-Product-Release-Version')
        results_str=results.read()
        results_lt=re.split(r":",results_str)
        jboss_version=results_lt[1].strip('\n')
    except Exception as e:
        jboss_version=""

    return jboss_version
    results.close()

def python_version_get():
    try:
        results=subprocess.Popen('python -V',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
        results_str=results.stderr.read().strip('\n')
        results_lt=re.split(r" ",results_str)
        python_version=results_lt[1]
    except Exception as e:
        python_version=""

    return python_version
    results.close()

def main():
    oracle_version=oracle_version_get()
    mysql_version=mysql_version_get()
    jboss_version=jboss_version_get()
#    python_version=python_version_get()
    print json.dumps({
        'oracle_version': oracle_version,
        'mysql_version': mysql_version,
        'jboss_version': jboss_version,
#        'python_version': python_version
         }
         )


if __name__=='__main__':
    main()

